package yxy.game.pm2.command.therionHunting;

import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TherionHuntingMessage;
import yxy.game.pm2.module.therionHunting.TherionChallengingPosition;
import yxy.game.pm2.module.therionHunting.TherionHandle;
import yxy.game.pm2.module.therionHunting.TherionPlayerModule;

import java.util.HashMap;

/**
 * @name CMD_TherionHuntingPositionChallenge
 * @author wrs
 * @descrition 神兽猎场归属位挑战
 * @since 2022-06-14
 */
@MessageType(proto = Command.TherionHuntingPositionChallenge ,classtype = TherionHuntingMessage.TherionHuntingPositionChallengeRequest.class)
public class CMD_TherionHuntingPositionChallenge extends Command<TherionHuntingMessage.TherionHuntingPositionChallengeRequest> {

    @Override
    public void execute(MessageSource messageSource, TherionHuntingMessage.TherionHuntingPositionChallengeRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int gameType = request.getGameType(); //玩法类型；1：本服神兽争夺，2：跨服神兽猎场，3：跨服世界神兽
        int therionId = request.getTherionId(); //神兽ID
        int positionId = request.getPositionId(); //挑战位Id
        String pCode = request.getPCode(); //对手的playerCode

        TherionHuntingMessage.TherionHuntingPositionChallengeResponse.Builder response = TherionHuntingMessage.TherionHuntingPositionChallengeResponse.newBuilder();

        TherionHandle handle = new TherionHandle();

        try {
            if (playerCode.equals(pCode)){
                send(messageSource,response, Result.NotChallengeMyself);//无法挑战自己
                return;
            }

            int therionKey = handle.getTherionKey(therionId);
            if (therionKey == -1){
                send(messageSource,response, Result.ParamError);
                return;
            }

            //是否还有挑战次数
            TherionPlayerModule playerModule = TherionPlayerModule.get(playerCode);

            //判断是否在排行榜上，在排行榜上不需要检查 挑战次数
            boolean inRank = handle.isInRank(gameType, playerCode, therionKey);
            if (playerModule.getChallengeNum().get(gameType) == 0 && !inRank) {
                send(messageSource, response, Result.ChallebgeNumNotEnough);//挑战次数不足
                return;
            }

            TherionChallengingPosition oppoPosition = TherionChallengingPosition.get(gameType, pCode,therionKey,positionId);

            //可能点太快oppoPosition 为null
            if(oppoPosition == null ){
                send(messageSource,response, Result.Success);
                return;
            }

            long seconds = (DateTime.now().getMillis() - oppoPosition.getOccupyTime().getMillis()) / 1000; //玩家站在挑战位上的时间，秒
            if (seconds <= 10){ //前十秒，其他玩家无法挑战该玩家
                send(messageSource,response, Result.TherionBattleNoChallenge);//无法挑战该玩家，10秒后重试
                return;
            }

            TherionPlayerModule oppoPlayerModule = TherionPlayerModule.get(pCode);
            if (oppoPlayerModule == null){
                send(messageSource,response, Result.DataError);
                return;
            }

            //是否挑战过该玩家，并且挑战失败
            HashMap<String, DateTime> challengeCoolDownMap = oppoPlayerModule.getCoolDown().get(gameType);
            DateTime dateTime = challengeCoolDownMap.get(playerCode);
            //冷却时间小于10秒
            if (dateTime != null && (DateTime.now().getMillis() - dateTime.getMillis()) / 1000 <= 10){
                send(messageSource,response, Result.TherionBattleNoChallenge);//无法挑战该玩家，10秒后重试
                return;
            }

            //挑战处理
            Result result = handle.battleExecute(playerCode,pCode,gameType,therionKey,response,playerModule,handle);
            if (!result.equals(Result.Success)){
                send(messageSource,response,result);
            }

            send(messageSource,response, Result.Success);
        }catch (Exception e){
            e.printStackTrace();
            send(messageSource,response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, TherionHuntingMessage.TherionHuntingPositionChallengeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
